/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reproductor.java
 *
 * Created on 03-ene-2012, 8:43:42
 */

package GUI;


import Control.PlayList;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.DefaultListModel;

/**
 *
 * @author Administrador
 */
public class ReproductorJF extends javax.swing.JFrame {

    /** Creates new form Reproductor */
    public ReproductorJF() {
        initComponents();
        d  = new Dimension(pantalla.getWidth(),pantalla.getHeight());
        modelo =   new DefaultListModel();
        playList.setModel(modelo);
        volumenJS.enable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pantalla = new javax.swing.JPanel();
        listaJP = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        playList = new javax.swing.JList();
        agregarJB = new javax.swing.JButton();
        removerJB = new javax.swing.JButton();
        controlesJP = new javax.swing.JPanel();
        playJB = new javax.swing.JButton();
        muteJB = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        volumenJS = new javax.swing.JSlider();
        progresoJPB = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pantalla.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout pantallaLayout = new javax.swing.GroupLayout(pantalla);
        pantalla.setLayout(pantallaLayout);
        pantallaLayout.setHorizontalGroup(
            pantallaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 471, Short.MAX_VALUE)
        );
        pantallaLayout.setVerticalGroup(
            pantallaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 521, Short.MAX_VALUE)
        );

        listaJP.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        playList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        playList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                playListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(playList);

        agregarJB.setText("Agregar ");
        agregarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarJBActionPerformed(evt);
            }
        });

        removerJB.setText("Remover");
        removerJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout listaJPLayout = new javax.swing.GroupLayout(listaJP);
        listaJP.setLayout(listaJPLayout);
        listaJPLayout.setHorizontalGroup(
            listaJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(listaJPLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(listaJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(listaJPLayout.createSequentialGroup()
                        .addComponent(agregarJB, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removerJB, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                .addContainerGap())
        );
        listaJPLayout.setVerticalGroup(
            listaJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, listaJPLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(listaJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(agregarJB)
                    .addComponent(removerJB))
                .addContainerGap())
        );

        controlesJP.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        playJB.setText("Play");
        playJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playJBActionPerformed(evt);
            }
        });

        muteJB.setText("Mute");
        muteJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                muteJBActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Info Video"));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 156, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jLabel1.setText("Volumen");

        volumenJS.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                volumenJSStateChanged(evt);
            }
        });

        javax.swing.GroupLayout controlesJPLayout = new javax.swing.GroupLayout(controlesJP);
        controlesJP.setLayout(controlesJPLayout);
        controlesJPLayout.setHorizontalGroup(
            controlesJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controlesJPLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(controlesJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlesJPLayout.createSequentialGroup()
                        .addComponent(playJB, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(muteJB, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1)
                    .addComponent(volumenJS, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                .addContainerGap())
        );
        controlesJPLayout.setVerticalGroup(
            controlesJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controlesJPLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(controlesJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(playJB, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(muteJB, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(volumenJS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(progresoJPB, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                    .addComponent(pantalla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listaJP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(controlesJP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(pantalla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(progresoJPB, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(listaJP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(controlesJP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void agregarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarJBActionPerformed
        miplaylist.Dialog(modelo);
        System.out.println("TamaÃ±o modelo: " + modelo.getSize());
        if (modelo.getSize()>0){
            url = miplaylist.getLastURL(modelo.getSize()-1);
        }
    }//GEN-LAST:event_agregarJBActionPerformed

    private void playJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playJBActionPerformed
        if (modelo.getSize()>0){
        //si no hay ningun video en reproduccion
           if ((isPlaying==false)){
               isPlaying=true;
               playJB.setText("Stop");
               if(url.toString().endsWith("mp3")){
                   video = new PanelDeVideo(url);
               }else{
                   video = new PanelDeVideo(url, d);
                   pantalla.add(video);
                   
               }
               video.play(url);
               volumenJS.enable(true);
               video.startAnimation(progresoJPB,url);
           }
           else // sino
           {
               video.stop(url);
               isPlaying=false;
               playJB.setText("Play");
               volumenJS.enable(false);
               video.stopAnimation(progresoJPB);
           }
        }
    }//GEN-LAST:event_playJBActionPerformed

    private void muteJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_muteJBActionPerformed
        video.mute();
    }//GEN-LAST:event_muteJBActionPerformed

    private void volumenJSStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_volumenJSStateChanged
        float volumen = (float) (volumenJS.getValue() / 100.0);
        video.setVolumen(volumen);
    }//GEN-LAST:event_volumenJSStateChanged

    private void removerJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerJBActionPerformed
        int q = playList.getSelectedIndex();
        if (q>=0)
        {
            String w = playList.getSelectedValues().toString();
            modelo.removeElementAt(q);
            miplaylist.removervideo(w);
            url = null;
        }
    }//GEN-LAST:event_removerJBActionPerformed

    private void playListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_playListMouseClicked
        if (modelo.getSize()>0){
            System.out.println("playlist: " + playList.getSelectedValue().toString());
            url  = miplaylist.getURL(playList.getSelectedValue().toString());
        }
    }//GEN-LAST:event_playListMouseClicked


    PanelDeVideo video;
    Dimension d;
    boolean isPlaying = false;
    URL url = null;
    String name="";
    DefaultListModel modelo ;
    PlayList miplaylist = new PlayList();



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ReproductorJF().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton agregarJB;
    private javax.swing.JPanel controlesJP;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel listaJP;
    private javax.swing.JButton muteJB;
    private javax.swing.JPanel pantalla;
    private javax.swing.JButton playJB;
    private javax.swing.JList playList;
    private javax.swing.JProgressBar progresoJPB;
    private javax.swing.JButton removerJB;
    private javax.swing.JSlider volumenJS;
    // End of variables declaration//GEN-END:variables

}
